home *** CD-ROM | disk | FTP | other *** search
- #include "windows.h"
-
- /*
- ** function to remove and reply all IntuiMessages on a port that have been
- ** sent to a particular hWnddow (note that we don't rely on the ln_Succ
- ** pointer of a message after we have replied it)
- */
- VOID StripIntuiMessages(struct MsgPort *hMsgPort, HWND hWnd)
- {
- struct IntuiMessage *hMsg = (struct IntuiMessage *) hMsgPort->mp_MsgList.lh_Head;
- struct Node *hNext;
-
- while (hMsg && (hNext = hMsg->ExecMessage.mn_Node.ln_Succ))
- {
- if (hMsg -> IDCMPWindow == hWnd)
- {
- /* Intuition is about to free this message.
- ** Make sure that we have politely sent it back.
- */
- Remove((struct Node *) hMsg);
-
- ReplyMsg((struct Message *) hMsg);
- }
- hMsg = (struct IntuiMessage *)hNext;
- }
- }
-
- /*
- ** Entry point to CloseWindowSafely()
- ** Strip all IntuiMessages from an IDCMP which are waiting for a specific
- ** hWnddow. When the messages are gone, set the UserPort of the hWnddow to
- ** NULL and call ModifyIDCMP(hWnd,0). This will free the Intuition arts of
- ** the IDMCMP and trun off message to this port without changing the
- ** original UserPort (which may be in use by other hWnddows).
- */
- VOID CloseWindowSafely(HWND hWnd)
- {
- if (!hWnd) return;
-
- /* we forbid here to keep out of race conditions with Intuition */
- Forbid();
-
- /* send back any messages for this hWnddow that have not yet been
- ** processed
- */
- StripIntuiMessages(hWnd->UserPort, hWnd);
-
- /* clear UserPort so Intuition will not free it */
- hWnd->UserPort = NULL;
-
- /* tell Intuition to stop sending more messages */
- ModifyIDCMP(hWnd, 0L);
-
- /* turn multitasking back on */
- Permit();
-
- /* Now it's safe to really close the hWnddow */
- CloseWindow(hWnd);
- }
-